/* eslint-disable array-callback-return */

export default {
  getEvents() {
    return {
      'edge:mouseover': 'onMouseover',
      'edge:mouseleave': 'onMouseleave',
      'edge:click': 'onClick'
    };
  },
  onMouseover(e) {
    const self = this;
    const item = e.item;
    const graph = self.graph;
    if (item.hasState('selected')) {
      return;
    }
    if (self.shouldUpdate.call(self, e)) {
      graph.setItemState(item, 'hover', true);
    }

    graph.paint();
  },
  onMouseleave(e) {
    const self = this;
    const item = e.item;
    const graph = self.graph;
    const group = item.getContainer();
    group.find(g => {
      if (g.attrs.isInPoint || g.attrs.isOutPoint) {
        g.attr('fill', '#fff');
      }
    });
    if (self.shouldUpdate.call(self, e)) {
      if (!item.hasState('selected')) graph.setItemState(item, 'hover', false);
    }
    graph.paint();
  },
  onClick(e) {
    const self = this;
    const item = e.item;
    const graph = self.graph;
    const autoPaint = graph.get('autoPaint');
    graph.setAutoPaint(false);
    const selectedNodes = graph.findAllByState('node', 'selected');
    // 取消选择的节点
    for (let i = 0, len = selectedNodes.length; i < len; i++) {
      const node = selectedNodes[i];
      graph.setItemState(node, 'selected', false);
    }

    if (!self.keydown || !self.multiple) {
      const selected = graph.findAllByState('edge', 'selected');
      for (let i = 0, len = selected.length; i < len; i++) {
        const edge = selected[i];
        graph.setItemState(edge, 'selected', false);
      }
    }
    if (item.hasState('selected')) {
      if (self.shouldUpdate.call(self, e)) {
        graph.setItemState(item, 'selected', false);
      }

      // this.getCompBox()
      //   .getCompMergeName(this.getCompID(),`editToolbar`)
      //   .nodeSelectChange({ target: item, select: false });
    } else {
      if (self.shouldUpdate.call(self, e)) {
        graph.setItemState(item, 'selected', true);
      }

      // this.getCompBox()
      //   .getCompMergeName(this.getCompID(),`editToolbar`)
      //   .nodeSelectChange({ target: item, select: true });
    }
    graph.setAutoPaint(autoPaint);
    graph.paint();
  }
};
